package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.helper.WCallBack;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.CheckPayPassActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.CheckPayPassActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.CancelSureDialog;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.ForgotPayPassDialog;
import com.wushi.lenovo.asia5b.app_widget.pass.PayPwdEditText;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/3/1.
 * 检测支付密码
 */
public class CheckPayPassActivity extends BaseMvpActivity<
        CheckPayPassActivityView, CheckPayPassActivityPresenter> implements CheckPayPassActivityView {
    @BindView(R.id.paypwd_checkpaypass_old)
    PayPwdEditText paypwd_checkpaypass_old;
    @BindView(R.id.tv_checkpaypass_forget)
    TextView tv_checkpaypass_forget;
    @BindView(R.id.bt_checkpaypass_submit)
    Button bt_checkpaypass_submit;
    @BindView(R.id.tv_checkpaypass_paypass_error)
    TextView tv_checkpaypass_paypass_error;
    @BindView(R.id.ll_checkpaypass_nothas)
    LinearLayout ll_checkpaypass_nothas;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_checkpaypass;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        paypwd_checkpaypass_old.initStyle(R.drawable.edit_num_bg, 6, 0.33f, R.color.common_text_black, R.color.common_text_black, 20);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        paypwd_checkpaypass_old.setOnTextChangeListener(content -> {
            tv_checkpaypass_paypass_error.setVisibility(View.GONE);
        });
    }

    @OnClick({
            R.id.bt_checkpaypass_submit,
            R.id.tv_checkpaypass_forget
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_checkpaypass_submit://继续修改支付密码
                String pwdText = paypwd_checkpaypass_old.getPwdText();
                if (TextUtils.isEmpty(pwdText) || pwdText.length() != 6) {
                    tv_checkpaypass_paypass_error.setVisibility(View.VISIBLE);
                    return;
                }
                CancelSureDialog dialog = new CancelSureDialog.Builder(this)
                        .setLeftContent(R.string.yes)
                        .setRightContent(R.string.cancel)
                        .setContent(getString(R.string.aaa1))
                        .build();
                dialog.show();
                dialog.setCallBack(new WCallBack() {
                    @Override
                    public void callback(Object o) {
                        getPresenter().checkCurrentPayPass(pwdText);
                    }
                });

                break;
            case R.id.tv_checkpaypass_forget:
                ForgotPayPassDialog forgotPayPassDialog = new ForgotPayPassDialog(this);
                forgotPayPassDialog.show();
                break;
            default:
                break;
        }
    }


    @Override
    public void checkCurrentPayPassResult(ResultModel resultModel) {
        if (resultModel.isSuccess()) {
            Intent intent = new Intent(this, FirstPayPassActivity.class);
            startActivity(intent);
        } else {
            paypwd_checkpaypass_old.clearText();
            tv_checkpaypass_paypass_error.setVisibility(View.VISIBLE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeProfileEvent(ChangeProfileEvent event) {
        if (event != null) {
            if (event.changeProfileType == ChangeProfileType.ChangePayPass) {
                finish();
            }
        }


    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
